package com.yuanfengerp.basicInfoManage.supplierInfo.service.impl;

import com.yuanfengerp.basicInfoManage.supplierInfo.dao.ISupplierinfoDao;
import com.yuanfengerp.basicInfoManage.supplierInfo.model.Supplierinfo;
import com.yuanfengerp.basicInfoManage.supplierInfo.service.ISupplierinfoservice;
import com.yuanfengerp.publicUtil.Result;
import com.yuanfengerp.publicUtil.SnowflakeIdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.sql.Timestamp;

@Service
public class SupplierinfoImpl implements ISupplierinfoservice {
    @Autowired
    ISupplierinfoDao isld;
    @Override
    public Result<List<Supplierinfo>> selectSupplierAll(Map<String, Object> params) {
        params.put("pagenum",((int) params.get("pagenum")-1) * (int) params.get("num"));
        List<Supplierinfo> supplierinfos = isld.selectSupplierAll(params);
        if (!(supplierinfos.isEmpty()))
            return Result.success(supplierinfos, isld.Count());
        return Result.failure(400,"没有这条信息");
    }

    @Override
    public Result<String> addSupplier(Supplierinfo supplierinfo) {
        SnowflakeIdWorker idWorker = new SnowflakeIdWorker(0);
        supplierinfo.setSupplier_id(idWorker.nextId());
        supplierinfo.setSupplier_time(new Timestamp(System.currentTimeMillis()));
        supplierinfo.setSupplier_amendtime(new Timestamp(System.currentTimeMillis()));
        Integer integer = isld.addSupplier(supplierinfo);
        if (integer > 0)
            return Result.success("添加成功");
        return Result.failure(400,"添加失败");
    }

    @Override
    public Result<String> updateSupplier(Supplierinfo supplierinfo) {
        supplierinfo.setSupplier_amendtime(new Timestamp(System.currentTimeMillis()));
        Integer integer = isld.updateSupplier(supplierinfo);
        if (integer > 0)
            return Result.success("修改成功");
        return Result.failure(400,"修改失败");
    }

    @Override
    public Result<Supplierinfo> selectSupplier(String id) {
        Supplierinfo supplierinfo = isld.selectSupplier(id);
        if (supplierinfo != null)
            return Result.success(supplierinfo);
        return Result.failure(400,"没找到");
    }
}
